package models

import "github.com/jinzhu/gorm"

type OutOrder struct {
	Model
	Type        int              `json:"type"`
	OrderStatus int              `json:"order_status"`
	Code        string           `json:"code"`
	TotalPrice  float64          `json:"total_price"`
	TotalNum    int              `json:"total_num"`
	CreateUser  string           `json:"create_user"`
	StorageId   int              `json:"storage_id"`
	StorageName string           `json:"storage_name"`
	Linkman     string           `json:"linkman"`
	LinkmanTel  string           `json:"linkman_tel"`
	Address     string           `json:"address"`
	Remark      string           `json:"remark"`
	Status      int              `json:"status"`
	OrderDetail []OutOrderDetail `json:"order_detail"`
}

func AddOutOrder(data map[string]interface{}) (id int) {
	res := db.Create(&OutOrder{
		Type:        data["type"].(int),
		Code:        data["code"].(string),
		TotalPrice:  data["total_price"].(float64),
		TotalNum:    data["total_num"].(int),
		CreateUser:  "admin",
		StorageId:   data["storage_id"].(int),
		StorageName: data["storage_name"].(string),
		Linkman:     data["linkman"].(string),
		LinkmanTel:  data["linkman_tel"].(string),
		Address:     data["address"].(string),
		Remark:      data["remark"].(string),
	})
	return res.Value.(*OutOrder).ID
}

func EditOutOrder(id int, data map[string]interface{}) bool {
	res := db.Model(&OutOrder{}).Where("id=?", id).Update(&OutOrder{
		TotalPrice:  data["total_price"].(float64),
		TotalNum:    data["total_num"].(int),
		OrderStatus: data["order_status"].(int),
		Remark:      data["remark"].(string),
	}).Error

	if res != nil {
		return false
	}

	outOrder, _ := GetOUtOrder(id)
	for _, v := range outOrder.OrderDetail {
		DecrementStockNum(outOrder.StorageId, v.ProductId, v.Num)
	}

	return true
}

func DeleteOutOrderById(id int) bool {
	err := db.Model(&OutOrder{}).Where("id=?", id).Update("status", 0).Error
	if err != nil {
		return false
	}
	return true
}

func ExistOutOrderById(id int) bool {
	var outOrder OutOrder
	db.Where("id=?", id).First(&outOrder)
	if outOrder.ID > 0 {
		return true
	}
	return false
}

func GetOUtOrder(id int) (*OutOrder, error) {
	var outOrder OutOrder
	err := db.Where("id=? and status=?", id, 1).First(&outOrder).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	err = db.Model(&outOrder).Related(&outOrder.OrderDetail).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return &outOrder, nil
}

func GetOutOrderList(pageNum int, pageSize int, maps interface{}) ([]*OutOrder, error) {
	var outOrders []*OutOrder
	//err := db.Preload("OrderDetail").Where(maps).Offset(pageNum).Limit(pageSize).Find(&outOrders).Error
	err := db.Where(maps).Where("status=?", 1).Offset(pageNum).Limit(pageSize).Find(&outOrders).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return outOrders, nil
}

func GetOutOrderTotal(maps interface{}) (int, error) {
	var count int
	err := db.Model(&OutOrder{}).Where(maps).Count(&count).Error
	if err != nil {
		return 0, err
	}
	return count, nil
}
